package ssm.Test.JavaMain;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.InputStream;
/**
 * @Author Maple.Chen
 * @Description 工厂类获取工厂线程池/会话的工具类
 * @Date 15:14 2020/4/24
 **/
public class MybatisUtil {
    private static SqlSessionFactory sessionFactory;
    static {
        try {
            //1.加载或读取mybatis配置文件 Configuration类，默认放src下，可以直接写名字
            InputStream input = Resources.getResourceAsStream("mybatis-config.xml");
            //2.使用SqlSessionFactoryBuilder创建SQLSessionFactory实例
            sessionFactory = new SqlSessionFactoryBuilder().build(input);
            input.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    public static SqlSession getSqlSession() {
        //3.产生SqlSession对象
        return sessionFactory.openSession();
    }
}
